package cn.linlane.service.sys;

import cn.linlane.entity.errors.MyError;
import cn.linlane.entity.exception.MyException;
import cn.linlane.entity.sys.SysUser;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


/**
 * @author cheng
 * @date 2018-07-04 14:34
 */
@Service
public class ShiroService {

    @Autowired
    private AuthorizingRealm realm;

    public SysUser getSysUser(){
        Subject subject = SecurityUtils.getSubject();

        if (subject == null || !subject.isAuthenticated()) {
            throw new MyException(MyError.USER_NOT_LOGIN);
        }

        SysUser sysUser = (SysUser) subject.getPrincipals().fromRealm("myRealm").iterator().next();

        if (sysUser != null){
            return sysUser;
        }else {
            throw new MyException(MyError.SYSTEM_ERROR);
        }
    }

}
